package pe.com.cineplanet.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
import org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver;

import pe.com.cineplanet.bean.cineplanet.Cartelera;
import pe.com.cineplanet.bean.cineplanet.Cine;
import pe.com.cineplanet.bean.cineplanet.Pelicula;
import pe.com.cineplanet.dao.dao.CarteleraDAO;
import pe.com.cineplanet.dao.dao.CineDAO;
import pe.com.cineplanet.dao.dao.PeliculaDAO;
import pe.com.cineplanet.form.CarteleraForm;

public class CarteleraController extends MultiActionController {

	private CarteleraForm form;
	private CarteleraDAO carteleraDAO;
	private PeliculaDAO peliculaDAO;
	private CineDAO cineDAO;
	
	public void setCarteleraDAO(CarteleraDAO carteleraDAO) {
		this.carteleraDAO = carteleraDAO;
	}
	
	public void setCineDAO(CineDAO cineDAO) {
		this.cineDAO = cineDAO;
	}
	
	public void setPeliculaDAO(PeliculaDAO peliculaDAO) {
		this.peliculaDAO = peliculaDAO;
	}
	
	private Class<?> commandClass;  
	
	public CarteleraController()
    {
        ParameterMethodNameResolver resolver = new ParameterMethodNameResolver();
        resolver.setParamName("method");
        resolver.setDefaultMethodName("view");
        setMethodNameResolver(resolver);
    }
	
	
	public ModelAndView view(HttpServletRequest request, HttpServletResponse response)
    throws Exception {
		return new ModelAndView("cartelera/view");
    } 
	
	public ModelAndView selectType(HttpServletRequest request, HttpServletResponse response)
    throws Exception {

		ModelAndView view = new ModelAndView("cartelera/combo");
		
		 getForm(request);
		 
		 String searchBy = form.getSearchBy();
		 if(searchBy.equals("PE")){
			 List<Pelicula> peliculas = carteleraDAO.findPeliculasCartelera();
			 view.addObject("searchValues", peliculas);
			 view.addObject("searchType", "PE");
		 }else if(searchBy.equals("CI")){
			 List<Cine> cines = cineDAO.list();
			 view.addObject("searchValues", cines);
			 view.addObject("searchType", "CI");
		 }
				 
		return view;
		
    } 
	
	public ModelAndView buscar(HttpServletRequest request, HttpServletResponse response)
    throws Exception {
		ModelAndView view = new ModelAndView("cartelera/detail");
		
		getForm(request);
		
		String searchBy = form.getSearchBy();
		String searchValue = form.getSearchValue();
		
		List<Cartelera> cartelera = null;
		
		if(searchBy==null || searchBy.equals("PE")){
			 
			cartelera = carteleraDAO.findByPelicula(searchValue);
			
		 }else if(searchBy.equals("CI")){

			cartelera = carteleraDAO.findByCine(searchValue);
			 
		 }
		
		/*List<Pelicula> peliculas = peliculaDAO.findLast();*/
		
		view.addObject("listCartelera", cartelera);
		view.addObject("pages", 1);
		view.addObject("lastpage", 1);
		
		return view;
		
    } 
	
	public ModelAndView pelicula(HttpServletRequest request, HttpServletResponse response)
    throws Exception {
		
		ModelAndView view = new ModelAndView("cartelera/pelicula");
		
		getForm(request);
		
		Pelicula pelicula = peliculaDAO.findById(form.getPeliculaId());
		pelicula.setArtistas( peliculaDAO.listActores(pelicula) );
				
		view.addObject("pelicula", pelicula);
		
		return view;
		
    } 
	
	
	
	/** Repetir si es que utiliza FORM en otros controllers ***/
	
	public Object getCommandObject(HttpServletRequest request)
	throws Exception {
		Object command = formBackingObject(request);
		bind (request, command);
		return command;
	}
	
	protected Object formBackingObject(HttpServletRequest request)
	throws Exception {
		return commandClass.newInstance();
	}

	public Class<?> getCommandClass() {
		return commandClass;
	}

	public void setCommandClass(Class<?> commandClass) {
		this.commandClass = commandClass;
	}

	public CarteleraForm getForm(HttpServletRequest request) throws Exception {
		Object command = getCommandObject(request);
    	form = (CarteleraForm)command;
		return form;
	}
	
	
	
}